/**
 * FileName: AccessDaoImpl.java
 * 员工角色权限Dao实现类
 *
 * @author  王威
 * @Date    2021/11/28
 * @version 1.00
 **/
package com.site.dao.impl;
import com.site.dao.AccessDao;
import com.site.pojo.Access;
import com.site.pojo.Employee;
import com.site.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class AccessDaoImpl implements AccessDao {
    /*查询所有员工角色权限*/
    @Override
    public List<Access> selectAll() throws SQLException {
        List<Access> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        ResultSet rst = JDBCUtils.executeQuery(conn, "SELECT * FROM `employee`");
        while (rst.next()){
            Access access = new Access();
            access.setAid(rst.getLong("aid"));
            access.setRootName(rst.getString("rootname"));
            access.setUrl(rst.getString("url"));
            list.add(access);
        }
        JDBCUtils.close(rst,null,conn);
        return list;
    }

    /*根据id查询员工角色权限所有信息*/
    @Override
    public Access selectById(Access access) throws SQLException {
        Access access1 = null;
        Connection conn = JDBCUtils.getConnection();
        ResultSet rst = JDBCUtils.executeQuery(conn, "SELECT * FROM `employee` where aid = ?",access.getAid());
        while (rst.next()){
            access1 = new Access();
            access1.setAid(rst.getLong("aid"));
            access1.setRootName(rst.getString("rootname"));
            access1.setUrl(rst.getString("url"));
        }
        JDBCUtils.close(rst,null,conn);
        return access1;
    }

    /*添加员工角色权限*/
    @Override
    public int addAccess(Connection con, Access access) throws SQLException {
        String sql = "insert into ` hurricane_house `.` access ` (` aid `, ` root_name `, ` url `)values(?,?,?);";
        Object[] prmas = {access.getAid(),access.getRootName(),access.getUrl()};
        return JDBCUtils.executeUpdate(con,sql,prmas);
    }
    /*删除员工角色数据*/
    @Override
    public int delAccess(Connection con, Access access) {
        String sql = "delete from ` hurricane_house `.` access ` where eid  =?";
        return JDBCUtils.executeUpdate(con,sql,new Object[]{access.getAid()});
    }
    /*修改员工角色权限*/
    @Override
    public int updateAccess(Connection con, Access access) {
        String sql = "update ` hurricane_house `.` access ` set ` root_name ` = ?, ` url ` = ? where ` aid ` = ?";
        Object[] prmas = {access.getRootName(),access.getUrl(),access.getAid()};
        return JDBCUtils.executeUpdate(con,sql,prmas);
    }
}
